package com.ruicar.afs.cloud.basic.common.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.entity.*;
import com.ruicar.afs.cloud.basic.common.service.*;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 客户信息查询
 */
@Slf4j
@RestController
@RequestMapping("/basicCustInfo")
@AllArgsConstructor
public class BasicCustInfoController {

    private final BasicCustPersonalDetailService basicCustPersonalDetailService;
    private final BasicContractCustRelationService basicContractCustRelationService;
    private final BasicCustBaseInfoService basicCustBaseInfoService;
    private final BasicCustContactDetailsService basicCustContactDetailsService;
    private final BasicCustAddressDetailsService basicCustAddressDetailsService;

    @ApiOperation(value = "根据合同号码查询主借人/共借人/担保人基本信息（custRole参数不传默认查询主借人的基本信息）")
    @PostMapping("/custBaseInfo")
    public IResponse<BasicCustBaseInfo> custBaseInfo(@RequestParam("contractNo") String contractNo,
                                                     @RequestParam(value="custRole",defaultValue="MAIN_PERSON") String custRole){
        if(EmptyUtils.isEmpty(contractNo)){
            return IResponse.fail("合同号码不允许为空");
        }
        BasicContractCustRelation basicContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery().
                eq(BasicContractCustRelation::getContractNo, contractNo)
                .eq(BasicContractCustRelation::getCustRole, custRole));
        if(EmptyUtils.isNotEmpty(basicContractCustRelation)){
            BasicCustBaseInfo basicCustBaseInfo = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaQuery().
                    eq(BasicCustBaseInfo::getCustNo, basicContractCustRelation.getCustNo()));
            basicCustBaseInfo.setCustRole(basicContractCustRelation.getCustRole());
            return IResponse.success(basicCustBaseInfo);
        }else{
            return IResponse.fail("没有查询到客户信息");
        }
    }

    @ApiOperation(value = "根据合同号码查询主共担基本信息（多条数据，列表格式）")
    @PostMapping("/custBaseInfoList")
    public IResponse<List<BasicCustBaseInfo>> custBaseInfoList(@RequestParam("contractNo") String contractNo){
        if(EmptyUtils.isEmpty(contractNo)){
            return IResponse.fail("合同号码不允许为空");
        }
        List<BasicContractCustRelation> basicContractCustRelations = basicContractCustRelationService.list(Wrappers.<BasicContractCustRelation>lambdaQuery().
                eq(BasicContractCustRelation::getContractNo, contractNo));
        List<BasicCustBaseInfo> basicCustBaseInfos = new ArrayList<>();
        if(EmptyUtils.isNotEmpty(basicContractCustRelations)){
            List<String> custNos = new ArrayList<>();
            for(BasicContractCustRelation cust : basicContractCustRelations){
                custNos.add(cust.getCustNo());
            }
            List<BasicCustBaseInfo> list = basicCustBaseInfoService.list(Wrappers.<BasicCustBaseInfo>lambdaQuery().in(BasicCustBaseInfo::getCustNo, custNos));
            if(EmptyUtils.isNotEmpty(basicCustBaseInfos)){
                for(BasicCustBaseInfo base : list){
                    for(BasicContractCustRelation cust : basicContractCustRelations){
                        if(base.getCustNo().equals(cust.getCustNo())){
                            base.setCustRole(cust.getCustRole());
                        }
                    }
                }
                basicCustBaseInfos.addAll(list);
            }
        }
        return IResponse.success(basicCustBaseInfos);
    }

    @ApiOperation(value = "根据合同号码查询主借人/共借人/担保人详细信息（custRole参数不传默认查询主借人）")
    @PostMapping("/custDetailInfo")
    public IResponse<BasicCustPersonalDetail> custDetailInfo(@RequestParam("contractNo") String contractNo,
                                                             @RequestParam(value="custRole",defaultValue="MAIN_PERSON") String custRole){
        if(EmptyUtils.isEmpty(contractNo)){
            return IResponse.fail("合同号码不允许为空");
        }
        BasicContractCustRelation basicContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery().
                eq(BasicContractCustRelation::getContractNo, contractNo)
                .eq(BasicContractCustRelation::getCustRole, custRole));
        if(EmptyUtils.isEmpty(basicContractCustRelation)){
            return IResponse.fail("没有查询到客户信息");
        }
        BasicCustPersonalDetail basicCustPersonalDetail = basicCustPersonalDetailService.getOne(Wrappers.<BasicCustPersonalDetail>lambdaQuery().
                eq(BasicCustPersonalDetail::getCustNo, basicContractCustRelation.getCustNo()));
        return IResponse.success(basicCustPersonalDetail);
    }

    @ApiOperation(value = "根据合同号码查询主共担详细信息")
    @PostMapping("/custDetailInfoList")
    public IResponse<List<BasicCustPersonalDetail>> custDetailInfoList(@RequestParam("contractNo") String contractNo){
        if(EmptyUtils.isEmpty(contractNo)){
            return IResponse.fail("合同号码不允许为空");
        }
        List<BasicContractCustRelation> basicContractCustRelations = basicContractCustRelationService.list(Wrappers.<BasicContractCustRelation>lambdaQuery().
                eq(BasicContractCustRelation::getContractNo, contractNo));
        List<BasicCustPersonalDetail> basicCustBaseInfos = new ArrayList<>();
        if(EmptyUtils.isNotEmpty(basicContractCustRelations)){
            List<String> custNos = new ArrayList<>();
            for(BasicContractCustRelation cust : basicContractCustRelations){
                custNos.add(cust.getCustNo());
            }
            List<BasicCustPersonalDetail> list = basicCustPersonalDetailService.list(Wrappers.<BasicCustPersonalDetail>lambdaQuery().
                    in(BasicCustPersonalDetail::getCustNo, custNos));
            if(EmptyUtils.isNotEmpty(basicCustBaseInfos)){
                basicCustBaseInfos.addAll(list);
            }
        }
        return IResponse.success(basicCustBaseInfos);
    }

    @ApiOperation(value = "根据合同号码查询主共担联系电话信息（custRole参数不传默认查询主借人）")
    @PostMapping("/custContactInfoList")
    public IResponse<List<BasicCustContactDetails>> custContactInfoList(@RequestParam("contractNo") String contractNo,
                                                                        @RequestParam(value="custRole",defaultValue="MAIN_PERSON") String custRole){
        if(EmptyUtils.isEmpty(contractNo)){
            return IResponse.fail("合同号码不允许为空");
        }
        BasicContractCustRelation basicContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery().
                eq(BasicContractCustRelation::getContractNo, contractNo)
                .eq(BasicContractCustRelation::getCustRole, custRole));
        if(EmptyUtils.isEmpty(basicContractCustRelation)){
            return IResponse.fail("没有查询到客户信息");
        }
        List<BasicCustContactDetails> basicCustContactDetails = basicCustContactDetailsService.list(Wrappers.<BasicCustContactDetails>lambdaQuery().
                eq(BasicCustContactDetails::getCustNo, basicContractCustRelation.getCustNo()));
        return IResponse.success(basicCustContactDetails);
    }

    @ApiOperation(value = "根据合同号码查询主共担地址信息（custRole参数不传默认查询主借人）")
    @PostMapping("/custAddressInfoList")
    public IResponse<List<BasicCustAddressDetails>> custAddressInfoList(@RequestParam("contractNo") String contractNo,
                                                                        @RequestParam(value="custRole",defaultValue="MAIN_PERSON") String custRole){
        if(EmptyUtils.isEmpty(contractNo)){
            return IResponse.fail("合同号码不允许为空");
        }
        BasicContractCustRelation basicContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery().
                eq(BasicContractCustRelation::getContractNo, contractNo)
                .eq(BasicContractCustRelation::getCustRole, custRole));

        if(EmptyUtils.isEmpty(basicContractCustRelation)){
            return IResponse.fail("没有查询到客户信息");
        }
        List<BasicCustAddressDetails> basicCustAddressDetails = basicCustAddressDetailsService.list(Wrappers.<BasicCustAddressDetails>lambdaQuery().
                eq(BasicCustAddressDetails::getCustNo, basicContractCustRelation.getCustNo()));
        return IResponse.success(basicCustAddressDetails);
    }




}
